import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

class NewScaffTest extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = TextStyle(color: Colors.grey);
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text('App Them'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () {},
          ),
        ],
      ),
      drawer: Drawer(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.asset("dingweijiaozheng_4.png"),
                  Container(
                    margin: EdgeInsets.all(10),
                    child: Text(
                      'data',
                      style: TextStyle(fontSize: 32),
                    ),
                  ),
                ],
              ),
              padding: EdgeInsets.only(top: 20, left: 10),
            ),
            Container(
              margin: EdgeInsets.all(10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: Icon(
                      Icons.add,
                      color: Colors.red,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 20),
                    child: Text('Add account'),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.red,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text(
                '主页',
                style: textStyle,
              )),
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text(
                '人类',
                style: textStyle,
              )),
          BottomNavigationBarItem(
              icon: Icon(Icons.delete),
              title: Text(
                '删除',
                style: textStyle,
              )),
        ],
        currentIndex: 1,
        fixedColor: Colors.amber,
        onTap: (view) {
          print('zhangchen' + view.toString());
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {},
      ),
    );
  }
}

class NewScaffoldTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _NewScaffoldTestStatu();
  }
}

class _NewScaffoldTestStatu extends State<NewScaffoldTest>
    with SingleTickerProviderStateMixin {
  var _currentIndex = 0;
  var _listViewIndex = -1;
  TabController _controller;
  static const loadingTag = "##loading##"; //表尾标记
  var _words = <String>[loadingTag];
  List tabs = ["图片处理", "滑动组件", "ListView", "ListView1"];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = TabController(length: tabs.length, vsync: this);
    _retrieveData();
  }

  void _retrieveData() {
    Future.delayed(Duration(seconds: 2)).then((e) {
      _words.insertAll(
          _words.length - 1,
          //每次生成20个单词
          generateWordPairs().take(20).map((e) => e.asPascalCase).toList());
      setState(() {
        //重新构建列表
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = TextStyle(color: Colors.grey);
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text('App Them'),
        leading: Builder(builder: (context) {
          return IconButton(
              icon: Icon(Icons.dashboard),
              onPressed: () {
                Scaffold.of(context).openDrawer();
              });
        }),
        bottom: TabBar(
          isScrollable: true,
          controller: _controller,
          tabs: tabs.map((e) => Tab(text: e)).toList(),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () {},
          ),
        ],
      ),
      drawer: Drawer(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  FlutterLogo(
                    size: 100,
                  ),
                  Container(
                    margin: EdgeInsets.all(10),
                    child: Text(
                      'Flutter',
                      style: TextStyle(fontSize: 32),
                    ),
                  ),
                ],
              ),
              padding: EdgeInsets.only(top: 20, left: 10),
            ),
            Container(
              margin: EdgeInsets.all(10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: Icon(
                      Icons.add,
                      color: Colors.red,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 20),
                    child: Text(
                      'Add account',
                      style: TextStyle(fontSize: 15),
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.all(10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: Icon(
                      Icons.settings,
                      color: Colors.red,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 20),
                    child: Text(
                      'Manage account',
                      style: TextStyle(fontSize: 15),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        color: Colors.deepOrange,
        shape: CircularNotchedRectangle(),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            IconButton(icon: Icon(Icons.home)),
            SizedBox(), //中间位置空出
            IconButton(icon: Icon(Icons.business)),
          ],
        ),
      ),
      body: TabBarView(
        controller: _controller,
        children: tabs.map((e) {
          return Center(
            child: _widgetGet(e),
          );
        }).toList(),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {},
      ),
    );
  }

  Widget _widgetGet(String e) {
    var word = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var words = word.split("");
    Image image = Image.asset('geo1.jpg', width: 100);
    if (e == '图片处理') {
      return Center(
        child: Column(
          children: <Widget>[
            image,
            ClipOval(
              child: image,
            ),
            ClipRRect(
              child: image,
              borderRadius: BorderRadius.all(Radius.circular(10)),
            ),
          ],
        ),
      );
    } else if (e == '滑动组件') {
      return Scrollbar(
        child: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Align(
            alignment: FractionalOffset(0.5, 0),
            child: Column(
              children: words
                  .map((e) => Padding(
                        padding: EdgeInsets.all(10),
                        child: Text(
                          e,
                          style: TextStyle(color: Colors.green, fontSize: 32),
                        ),
                      ))
                  .toList(),
            ),
          ),
        ),
      );
    } else if ("ListView" == e) {
      return Scrollbar(
        child: NotificationListener(
          onNotification: (notification){
            switch (notification.runtimeType){
              case ScrollStartNotification: print("开始滚动"); break;
              case ScrollUpdateNotification: print("正在滚动"); break;
              case ScrollEndNotification: print("滚动停止"); break;
              case OverscrollNotification: print("滚动到边界"); break;
            }
            return false;
          },
          child: ListView.separated(
              key: PageStorageKey(1),
              physics: BouncingScrollPhysics(),
              separatorBuilder: (context, index) {
                return Divider(
                  color: Colors.deepOrange,
                );
              },
              itemCount: 500,
              itemBuilder: (context, index) {
                return ListTile(
                  leading: CircleAvatar(
                    backgroundImage: AssetImage('geo1.jpg'),
                  ),
                  title: Text("$index"),
                  subtitle: Text("列表第$index项"),
                  dense: true,
                  selected: _listViewIndex == index ? true : false,
                  trailing: Icon(Icons.keyboard_arrow_right),
                  onTap: () {
                    setState(() {
                      _listViewIndex = index;
                    });
                  },
                );
              }),
        ),
      );
    } else if ('ListView1' == e) {
      return Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.all(5),
            decoration: BoxDecoration(
              gradient:
                  LinearGradient(colors: [Colors.green, Colors.amberAccent]),
              boxShadow: [
                BoxShadow(
                    color: Colors.grey,
                    offset: Offset(2.0, 2.0),
                    blurRadius: 4.0),
              ],
            ),
            child: ListTile(
              title: Text(
                '英语单词',
                style: TextStyle(color: Colors.deepOrange, fontSize: 35),
              ),
            ),
          ),
          Expanded(
            child: ListView.separated(
                key: PageStorageKey(2),
                itemBuilder: (context, index) {
                  if (_words[index] == loadingTag) {
                    if (_words.length - 1 < 100) {
                      _retrieveData();
                      return Container(
                        padding: EdgeInsets.all(15),
                        alignment: Alignment.center,
                        child: SizedBox(
                          width: 20.0,
                          height: 20.0,
                          child: CircularProgressIndicator(
                            strokeWidth: 2.0,
                          ),
                        ),
                      );
                    } else {
                      return Container(
                        padding: EdgeInsets.all(15),
                        alignment: Alignment.center,
                        child: Text(
                          '没有更多了',
                          style: TextStyle(color: Colors.green),
                        ),
                      );
                    }
                  }
                  return ListTile(
                    title: Text(
                      _words[index],
                      style: TextStyle(color: Colors.deepOrange, fontSize: 25),
                    ),
                  );
                },
                separatorBuilder: (context, index) {
                  return Divider(
                    color: Colors.deepOrange,
                  );
                },
                itemCount: _words.length),
          ),
        ],
      );
    } else {
      return null;
    }
  }
}

class MyClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) => Rect.fromLTWH(10.0, 15.0, 40.0, 30.0);

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) => false;
}
